<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="商户号" prop="merId">
        <el-input
          v-model="queryParams.merId"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名称" prop="merName">
        <el-input
          v-model="queryParams.merName"
          placeholder="请输入商户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会代码" prop="businessNo">
        <el-input
          v-model="queryParams.businessNo"
          placeholder="请输入统一社会代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['merchant:merchant:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:merchant:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:merchant:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商户号" width="180" align="center" prop="merId"/>
      <el-table-column label="商户名称" width="280" align="center" prop="merName"/>

      <el-table-column label="统一社会代码" width="180" align="center" prop="businessNo"/>
      <el-table-column label="联系电话" width="180" align="center" prop="merLinktel"/>
      <el-table-column label="签约人" align="center" prop="signerName"/>
      <el-table-column label="开通日期" align="center" prop="openDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商户状态" align="center" prop="merStatus"
                       :formatter="merStatusFormat">
        <template slot-scope="scope">
          <span v-if="scope.row.merStatus === '1'"><el-tag type="success" effect="dark" size="mini">有效</el-tag></span>
          <span v-if="scope.row.merStatus === '2'"><el-tag type="danger" effect="dark" size="mini">无效</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:merchant:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:merchant:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户号" prop="merId">
          <el-input v-model="form.merId" placeholder="长度为6位, 企业以1开头，服务公司以2开头" :disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="商户名称" prop="merName">
          <el-input v-model="form.merName" placeholder="请输入商户名称" :disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="商户类型" prop="merType">
          <el-select v-model="form.merType" placeholder="请选择商户类型">
            <el-option
              v-for="dict in merTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户状态" prop="merStatus">
          <el-select v-model="form.merStatus" placeholder="请选择商户状态">
            <el-option
              v-for="dict in merStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统一社会代码" prop="businessNo">
          <el-input v-model="form.businessNo" placeholder="请输入统一社会代码"/>
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户"/>
        </el-form-item>
        <el-form-item label="银行账户名称" prop="bankAccountName">
          <el-input v-model="form.bankAccountName" placeholder="请输入银行账户名称"/>
        </el-form-item>
        <el-form-item label="商户地址" prop="merAddr">
          <el-input v-model="form.merAddr" placeholder="请输入商户地址"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="merLinktel">
          <el-input v-model="form.merLinktel" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="签约人" prop="signerName">
          <el-input v-model="form.signerName" placeholder="请输入签约人"/>
        </el-form-item>
        <el-form-item label="允许访问接口IP" prop="authIp">
          <el-input v-model="form.authIp" type="textarea" placeholder="多个用英文逗号隔开"/>
        </el-form-item>
        <el-form-item label="银行类型" prop="payChannel">
          <el-select filterable v-model="form.payChannel" placeholder="请选择银行类型">
            <el-option
              v-for="dict in bankTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户token" prop="token">
          <el-input v-model="form.token" placeholder="请输入商户token"/>
        </el-form-item>
        <el-form-item label="商户秘钥" prop="merKey">
          <el-input v-model="form.merKey" placeholder="请输入商户秘钥"/>
        </el-form-item>
        <el-form-item label="开通日期" prop="openDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.openDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择开通日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关闭日期" prop="closeDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.closeDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择关闭日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开票类型">
          <el-select v-model="form.invoiceType" placeholder="请选择开票类型">
            <el-option
              v-for="dict in invoiceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户费率" prop="merRate">
          <el-input v-model="form.merRate" placeholder="请输入商户费率"/>
        </el-form-item>
        <el-form-item label="短信渠道" prop="smsChannel">
          <el-input v-model="form.smsChannel" placeholder="请输入短信渠道"/>
        </el-form-item>
        <el-form-item label="短信费用" prop="smsFee">
          <el-input v-model="form.smsFee" placeholder="请输入短信费用"/>
        </el-form-item>
        <el-form-item label="鉴权渠道" prop="bankcardChannel">
          <el-input v-model="form.bankcardChannel" placeholder="请输入鉴权渠道"/>
        </el-form-item>
        <el-form-item label="鉴权手续费" prop="bankcardFee">
          <el-input v-model="form.bankcardFee" placeholder="请输入鉴权手续费"/>
        </el-form-item>
        <el-form-item label="身份证渠道" prop="idcardChannel">
          <el-input v-model="form.idcardChannel" placeholder="请输入身份证渠道"/>
        </el-form-item>
        <el-form-item label="身份证费用" prop="idcardFee">
          <el-input v-model="form.idcardFee" placeholder="请输入身份证费用"/>
        </el-form-item>
        <el-form-item label="代付费用" prop="vireFee">
          <el-input v-model="form.vireFee" placeholder="请输入代付费用"/>
        </el-form-item>
        <el-form-item label="商户介绍" prop="remarks">
          <el-input v-model="form.remarks" maxlength="60" type="textarea" placeholder="请输入内容(不超过60个字符)"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listMerchant,
    getMerchant,
    delMerchant,
    addMerchant,
    updateMerchant,
    exportMerchant
  } from "@/api/merchant/merchant";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        merIds: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 商户信息表格数据
        merchantList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //商户是否是编辑状态
        isEdit: true,
        // 商户类型(1-企业,2-服务公司)字典
        merTypeOptions: [],
        // 商户状态(1-有效,2-无效)字典
        merStatusOptions: [],
        // 开票类型 0：普票 1：专票字典
        invoiceTypeOptions: [],
        // 银行类型字典
        bankTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merId: undefined,
          merName: undefined,
          businessNo: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          merId: [
            {required: true, message: "商户号不能为空", trigger: "blur"},

          ], merName: [
            {required: true, message: "商户名称不能为空", trigger: "blur"}
          ], merType: [
            {required: true, message: "商户类型不能为空", trigger: "change"}
          ], merStatus: [
            {required: true, message: "商户状态不能为空", trigger: "change"}
          ], businessNo: [
            {required: true, message: "统一社会代码不能为空", trigger: "blur"},
            {pattern: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/, message: '统一社会代码格式不正确'}

          ], bankAccount: [
            {required: true, message: "银行账户不能为空", trigger: "blur"},
            {pattern: /^([1-9]{1})(\d{11}|\d{14}|\d{15}|\d{16}|\d{17}|\d{18}|\d{19})$/, message: '银行账户格式不正确'}
          ], bankAccountName: [
            {required: true, message: "银行账户名称不能为空", trigger: "blur"}
          ], merAddr: [
            {required: true, message: "商户地址不能为空", trigger: "blur"}
          ], merLinktel: [
            {required: true, message: "联系电话不能为空", trigger: "blur"},
            {pattern: /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/, message: '联系电话格式不正确'}
          ], payChannel: [
            {required: true, message: "下发渠道不能为空", trigger: "blur"}
          ], openDate: [
            {required: true, message: "开通日期不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("mer_type").then(response => {
        this.merTypeOptions = response.data;
      });
      this.getDicts("mer_status").then(response => {
        this.merStatusOptions = response.data;
      });
      this.getDicts("invoice_type").then(response => {
        this.invoiceTypeOptions = response.data;
      });
      this.getDicts("bank_type").then(response => {
        this.bankTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询商户信息列表 */
      getList() {
        this.loading = true;
        listMerchant(this.queryParams).then(response => {
          this.merchantList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 商户类型(1-企业,2-服务公司)字典翻译
      merTypeFormat(row, column) {
        return this.selectDictLabel(this.merTypeOptions, row.merType);
      },
      // 商户状态(1-有效,2-无效)字典翻译
      merStatusFormat(row, column) {
        return this.selectDictLabel(this.merStatusOptions, row.merStatus);
      },
      // 开票类型 0：普票 1：专票字典翻译
      invoiceTypeFormat(row, column) {
        return this.selectDictLabel(this.invoiceTypeOptions, row.invoiceType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          merId: undefined,
          merName: undefined,
          merType: undefined,
          merStatus: undefined,
          businessNo: undefined,
          bankAccount: undefined,
          bankAccountName: undefined,
          merAddr: undefined,
          merLinktel: undefined,
          signerName: undefined,
          authIp: undefined,
          payChannel: undefined,
          token: undefined,
          merKey: undefined,
          openDate: undefined,
          closeDate: undefined,
          invoiceType: undefined,
          merRate: undefined,
          smsChannel: undefined,
          smsFee: undefined,
          bankcardChannel: undefined,
          bankcardFee: undefined,
          idcardChannel: undefined,
          idcardFee: undefined,
          vireFee: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.merIds = selection.map(item => item.merId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isEdit = false;
        this.open = true;
        this.title = "添加商户信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.isEdit = true;
        const id = row.id || this.ids
        getMerchant(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商户信息";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateMerchant(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addMerchant(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const merIds = row.merId || this.merIds
        this.$confirm('是否确认删除数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delMerchant(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出商户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportMerchant(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
